package bakaserver.recipe;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import net.elseland.xikage.MythicMobs.Items.AttributeHandler;

import org.bukkit.ChatColor;

public class RecipeItem {
	private String strName, strPermName, strId;
	List<String> strReqLoreList, strItemLoreList, strCraftTable, strEnchants, strPotion;
	private double dDamage = -1.0D, dHealth = 0.0D, dKnock = 0.0D;
	private double fMoveSpeed;
	private int dwData = 0, dwId = 1, dAmount = 1;
	private boolean useIID = false;

	@SuppressWarnings("deprecation")
	public RecipeItem(YamlConfiguration cfg, int ItemID) {
		String strPath = "Items.Item" + ItemID + ".";

		strCraftTable = cfg.getStringList(strPath + "CraftTable");
		strPermName = cfg.getString(strPath + "PermName");
		strId = cfg.getString(strPath + "ID", "1");
		if (strId.matches("[0-9]*")) {
			useIID = true;
			dwId = Integer.parseInt(strId);
		}
		dwData = cfg.getInt(strPath + "Data", 0);
		strName = cfg.getString(strPath + "Name",
				useIID ? Material.getMaterial(dwId).name() : Material.getMaterial(strId).name());
		dAmount = cfg.getInt(strPath + "Count");
		dDamage = cfg.getDouble(strPath + "Damage");
		dHealth = cfg.getDouble(strPath + "Health");
		dKnock = cfg.getDouble(strPath + "Knock");
		fMoveSpeed = cfg.getDouble(strPath + "MovementSpeed");
		strReqLoreList = cfg.getStringList(strPath + "RequireLore");
		strItemLoreList = cfg.getStringList(strPath + "ItemLore");
		strEnchants = cfg.getStringList(strPath + "Enchants");
		strPotion = cfg.getStringList(strPath + "Potion");
	}

	public String getName() {
		return strName;
	}

	public String getPermName() {
		return strPermName;
	}

	public String[] getRepice() {
		return strCraftTable.toArray(new String[0]);
	}

	public int getID() {
		return dwId;
	}

	public int getAmount() {
		return dAmount;
	}

	public double getDamage() {
		return dDamage;
	}

	public double getHealth() {
		return dHealth;
	}

	public double getMoveSpeed() {
		return fMoveSpeed;
	}

	public double getKnock() {
		return dKnock;
	}

	public String[] getRequestLoreList() {
		return (String[]) strReqLoreList.toArray();
	}

	public String[] getLoreList() {
		return (String[]) strItemLoreList.toArray();
	}

	@SuppressWarnings("deprecation")
	public ItemStack getItemStack() {
		ItemStack stack;
		if (useIID) {
			stack = new ItemStack(Material.getMaterial(dwId), dAmount, (short) dwData);
		} else {
			stack = new ItemStack(Material.getMaterial(this.strId.toUpperCase()), dAmount, (short) dwData);
		}

		ItemMeta im = stack.getItemMeta();
		if (strItemLoreList.size() > 0)
			setLore(im, strItemLoreList);
		if (strEnchants.size() > 0)
			setEnchants(im, strEnchants);
		if (strPotion.size() > 0)
			buildPotion(im);
		if (strName != null)
			setDisplay(im, strName);

		stack.setItemMeta(im);
		if (dHealth != 0.0D)
			AttributeHandler.addHealth("最大生命+", stack, dHealth);
		if (dDamage != -1.0D)
			AttributeHandler.addDamage("伤害+", stack, dDamage);
		if (dKnock != 0.0D)
			AttributeHandler.addKnockBackRes("击退+", stack, dKnock);
		if (fMoveSpeed != 0.0D)
			AttributeHandler.addSpeed("移速+", stack, fMoveSpeed);

		return stack;
	}

	@SuppressWarnings("deprecation")
	public static ItemMeta setEnchants(ItemMeta im, List<String> enchants) {
		if (enchants == null)
			return im;

		for (String s : enchants) {
			if (s.contains(":")) {
				String[] part = s.split(":");

				if (part[0].matches("[0-9]*")) {
					im.addEnchant(Enchantment.getById(Integer.parseInt(part[0])), Integer.parseInt(part[1]), true);
				} else {
					Enchantment en = Enchantment.getByName(part[0]);
					if (en != null) {
						im.addEnchant(Enchantment.getByName(part[0]), Integer.parseInt(part[1]), true);
					}
				}
			}
		}

		return im;
	}

	public static ItemMeta setLore(ItemMeta im, List<String> lore) {
		if (lore.size() == 0)
			return im;
		List<String> list = new ArrayList<String>();

		for (String s : lore) {
			s = ChatColor.translateAlternateColorCodes('&', s);
			list.add(s);
		}
		if (list != null)
			im.setLore(list);
		return im;
	}

	public ItemMeta buildPotion(ItemMeta im) {

		((PotionMeta) im).clearCustomEffects();

		for (String s : this.strPotion) {
			try {
				String[] sp = s.split(" ");
				if (sp.length < 3) {
					recipe.DebugLogger("A potion effect for Item " + this.strName
							+ " is configured incorrectly. Format is 'EFFECT DURATION LEVEL'. Line=" + s);
					continue;
				}

				PotionEffectType pet = PotionEffectType.getByName(sp[0].toUpperCase());
				int duration = Integer.parseInt(sp[1]);
				int level = Integer.parseInt(sp[2]);

				PotionEffect p = new PotionEffect(pet, duration, level - 1, true);

				((PotionMeta) im).addCustomEffect(p, false);
			} catch (Exception ex) {
				recipe.DebugLogger("A potion effect for Item " + this.strName
						+ " is configured incorrectly. Format is 'EFFECT DURATION LEVEL'. Line=" + s);
			}
			continue;
		}

		return im;
	}

	public static ItemMeta setDisplay(ItemMeta im, String s) {
		s = ChatColor.translateAlternateColorCodes('&', s);
		im.setDisplayName(s);
		return im;
	}

}
